<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/8/19
 * Time: 16:09
 */

namespace app\controller\sys;

use app\model\Info AS model;
use app\controller\Base;
use app\model\InfoContent;
use think\facade\Db;
class Info extends Base
{
    public function save(){
        try {
            Db::startTrans();
            $id = input('id');
            $post = input('post.');
            $data = [
                'name' => $post['name'] ?? '',
                'image_id' => $post['image_id'] ?? 0,
                'info' => $post['info'] ?? '',
                'nav_id' => $post['nav_id'] ?? '',
                'lang' => $this->lang,
                'uniqid' => $this->getuniqid()
            ];
            if (empty($id)) {
                $find = model::create($data);
            } else {
                $find = model::find($id);
                $find->save($data);
            }
            if (!empty($post['content'])){
                $content = InfoContent::where('info_id',$find->id)->find();
                if (empty($content->id))
                    InfoContent::create([
                        'content' => $post['content'],
                        'info_id' => $find->id
                    ]);
                else
                    $content->save(['content' => $post['content']]);
            }
            Db::commit();
            return $this->msg($find);
        }catch (\Exception $e){
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }}